//
//  WYRedPacketMessageController+WYLogic.m
//  Boss
//
//  Created by admin on 2018/6/2.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "WYRedPacketMessageController+WYLogic.h"
#import "ZYMainController.h"
#import "WYBaseNavigationController.h"
#import "WYReportController.h"
@implementation WYRedPacketMessageController (WYLogic)
- (void)closeAllPresentedViewControllers {
    
    if (self.navigationController.topViewController == self) {
        [self.navigationController popViewControllerAnimated:YES]; return;
    } else {
//        [self dismissViewControllerAnimated:YES completion:nil];return;
    }
    
    if (![[UIApplication sharedApplication].keyWindow.rootViewController isKindOfClass:[ZYMainController class]]) {
        return;
    }
    ZYMainController *mainVC = (ZYMainController *)[UIApplication sharedApplication].keyWindow.rootViewController;
    WYBaseNavigationController *currentNav = mainVC.viewControllers[mainVC.selectedIndex];
    UIViewController *currentVC = currentNav.topViewController;
    
    //第一部分 导航控制器的顶部控制器弹出的模态
    UIViewController *vcPresentVC = currentVC.presentedViewController;
    if (vcPresentVC) {
        while (vcPresentVC.presentedViewController)  {
            vcPresentVC = vcPresentVC.presentedViewController;
        }
        [vcPresentVC dismissViewControllerAnimated:NO completion:nil];
    }
    
    //第二部分 导航控制器弹出的模态
    UIViewController *navPresentVC = currentNav.presentedViewController;
    if (navPresentVC) {
        while (navPresentVC.presentedViewController)  {
            navPresentVC = navPresentVC.presentedViewController;
        }
        [navPresentVC dismissViewControllerAnimated:NO completion:nil];
    }
    
    //第三部分 tab控制器弹出的模态
    UIViewController *tabPresentVC = mainVC.presentedViewController;
    if (tabPresentVC) {
        while (tabPresentVC.presentedViewController)  {
            tabPresentVC = tabPresentVC.presentedViewController;
        }
        [tabPresentVC dismissViewControllerAnimated:NO completion:nil];
    }
    self.presentingViewController.tabBarController.tabBar.hidden = NO;
}
-(void)changeTime
{
    __block NSInteger timeOut = 5;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    // 每秒执行一次
    dispatch_source_set_timer(_timer, dispatch_walltime(NULL, 0), 1.0 * NSEC_PER_SEC, 0);
    dispatch_source_set_event_handler(_timer, ^{
        
        // 倒计时结束，关闭
        if (timeOut <= 0) {
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                self.navigationController.interactivePopGestureRecognizer.enabled = YES;
                if ([self.redPacket.redpacketType isEqualToString:@"1"]) {
                    if (self.advertisementMessage.pageView) {
                          [self.leftBarButton setTitle:[NSString stringWithFormat:@"%@人看过",self.advertisementMessage.pageView] forState:(UIControlStateNormal)];
                    }
                  
                }else{
                    if (self.redMessage.pageView) {
                       [self.leftBarButton setTitle:[NSString stringWithFormat:@"%@人看过",self.redMessage.pageView] forState:(UIControlStateNormal)];
                    }
                    
                }
                
                [self.leftBarButton addTarget:self action:@selector(dismiss:) forControlEvents:(UIControlEventTouchUpInside)];
                self.leftBarButton.titleLabel.font = kFontSystem(18);
                
            });
            
        }else{
            
            int seconds = timeOut % 60;
            NSString * timeStr = [NSString stringWithFormat:@"%0.2d",seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.leftBarButton setTitle:[NSString stringWithFormat:@"%@秒后退出",timeStr] forState:UIControlStateNormal];
                
            });
            timeOut--;
        }
    });
    
    dispatch_resume(_timer);
}



-(void)showAlertAction
{
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:(UIAlertControllerStyleActionSheet)];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"举报" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        WYReportController *rpc = [[WYReportController alloc]init];
        
        [self.navigationController pushViewController:rpc animated:YES];
    }];
    [alertController addAction:action1];
    [alertController addAction:action2];
    
    
    if (IS_IPAD) {
        UIPopoverPresentationController *popPresenter = [alertController popoverPresentationController];
        popPresenter.barButtonItem = self.navigationItem.rightBarButtonItem;
        //   popPresenter.sourceRect = self.navigationItem.rightBarButtonIte;
        [self presentViewController:alertController animated:YES completion:nil];
        
    }else{
        [self presentViewController:alertController animated:YES completion:nil];
    }
    
}












@end
